var pageParams;
function addLoad(){
    initPerson();
}
//返回值为需不需要关闭窗口
function yesOperator(){
    var retValue=addData();
    return retValue;
}


//人员选择下拉列表初始化
function initPerson(){
    console.log("=initPerson=");
    $$.ajax({
        url:"/rest/BaseUserBusiness/list",
        suiteCode:"base",
        success : function(data){
           for(var i=0;i<data.length;i++){
             $("#b_manager").append("<option value=\""+data[i].id+"\" >"+data[i].userName+"</option>");
             $("#b_leader").append("<option value=\""+data[i].id+"\" >"+data[i].userName+"</option>");

           }
        }
    });

}

function initUpdateData(){
    console.log("=initPerson=");
    $$.ajax({
        url:"/rest/BaseUserBusiness/list",
        suiteCode:"base",
        success : function(data){
            for(var i=0;i<data.length;i++){
                $("#b_manager").append("<option value=\""+data[i].id+"\" >"+data[i].userName+"</option>");
                $("#b_leader").append("<option value=\""+data[i].id+"\" >"+data[i].userName+"</option>");

            }
            updateLoad();
        }
    });

}

function updateLoad(){
    console.log("=updateLoad=");
    var params = $$.getQueryString();
    $$.ajax({
        url:"/rest/BaseDeptBusiness/query",
        suiteCode:"base",
        data : {
            id : params.rowId
        },
        success : function(data){
            $("#b_id").val(data.id);
            $("#b_appCode").val(data.appCode);
            $("#b_pid").val(data.pid);
            $("#b_deptCode").val(data.deptCode);
            $("#b_deptName").val(data.deptName);
            console.log("data.manager:"+data.manager);
            console.log("b_manager.html:"+$("#b_manager").html());
            $("#b_manager").val(data.manager);
            console.log("b_manager:"+$("#b_manager").val());
            $("#b_leader").val(data.leader);
            $("#b_level").val(data.level);
            $("#b_isLeaf").val(data.isLeaf);
            $("#b_status").val(data.status);
            $("#b_rowSort").val(data.rowSort);
            $("#b_remarks").val(data.remarks);

            var status=data.status;
            $("input[name^='b_status_']").each(function(){
                if($(this).val()==status){
                    this.checked='checked';
                }
            });

            $("#b_deptCodeOld").val(data.deptCode);
        }
    });
}
function validForm(){
    return $('#theform').valid();
}
function addData(){
    var param = {};
    var validFormRet;
    $$.form({
        async:false,
        suiteCode:"base",
        able:function(){
            validFormRet=false;
        },
        checkForm: validForm,
        success: function () {
            validFormRet= true;
        }
    });
    return validFormRet;
}
function forwardCalcValid() {
    $("#theform").validate({
        rules: {
            b_appCode: {

                required: true,

                minlength: 1,
                number: false
            },
            b_pid: {

                required: true,

                minlength: 1,
                number: false
            },
            b_deptCode: {
                required: true,
                minlength: 1,
                number: false,
                isCode:true
            },
            b_deptName: {

                required: true,

                minlength: 1,
                number: false
            },
            b_manager: {

                required: true,

                minlength: 1,
                number: false
            },
            b_leader: {

                required: true,

                minlength: 1,
                number: false
            },
            b_level: {

                required: true,

                minlength: 1,
                number: false
            },
            b_isLeaf: {

                required: true,

                minlength: 1,
                number: false
            },
            b_status: {

                required: true,

                minlength: 1,
                number: false
            },

            b_rowSort: {
                required: true,
                minlength: 1,
                isIntGteZero: false,
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errortips",
        success: 'valid',
        messages: {
            b_appCode: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_pid: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_deptCode: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_deptName: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_manager: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_leader: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_level: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_isLeaf: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },
            b_status: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",
                number: "请输入数字",
            },

            b_rowSort:{
                required: " (必填字段)",
                minlength: " (不能少于1位编号)",
                isIntGteZero: "请输入数字",
            }
        },
    });
}

$(function () {
    pageParams=$$.getQueryString();

    // console.log("pageargs:"+JSON.stringify(pageParams));
    $("#b_pid").val(pageParams.rowId);

    forwardCalcValid();
    $("input[name^='b_status']").click(function(){
        $("#b_status").val($(this).val());
    });
});

jQuery.validator.addMethod("isCode", function(value, element) {
    //校验编码是否存在
    var deptCodeOld=$("#b_deptCodeOld").val();
    var deptCode=$("#b_deptCode").val();
    if(deptCodeOld==deptCode){
        return true;
    }else{
            var args={};
            args.c_deptCode_1=$("#b_deptCode").val();
            args.page=0;
            args.pageSize=5;
            args.isPage=1;
            var exists;
            $$.ajax({
                url: "/bootstrap/basegrid/BaseDeptBusiness/list",
                async:false,
                suiteCode:"base",
                data: args,
                success: function (data) {
                    if(data.length && data.length>0){
                        exists=true;
                     }else{
                        exists=false;
                    }
                }
            });
	        if(exists){
	            $(element).data('error-msg','编码不能重复！');
	            return false;
	        }
        return true;
    }
}, function(params, element) {
    return $(element).data('error-msg');
});
/**/
